var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		st: 'all',
		datalist: [],
		pagenum: 1
	},
	_onLoad: function () {
		var that = this;
		if (that.options.st) {
			that.setData({ st: that.options.st })
		}
		that.setData({ pagenum: 1, st: that.data.st, datalist: [], $nomoreData: false })
		$.get('peisong/orderlist',{st: that.data.st, pagenum: 1}, function (res) {
			if(res.status==0){
				$.alert(res.msg)
			}else{
				that.setData(res)
				that.getDataList();
			}
		})
	},
	changetab: function (e) {
		var st = e.currentTarget.dataset.st
		this.setData({ pagenum: 1, st: st, datalist: [], $nomoreData: false })
		wx.pageScrollTo({
			scrollTop: 0, duration: 0
		})
		this.getDataList()
	},
	getDataList: function () {
		var that = this;
		var st = that.data.st
		var pagenum = that.data.pagenum;
		$.post('peisong/orderlist', { st: st, pagenum: pagenum }, function (res) {
			var data = res.data;
			if (pagenum == 1) {
				that.setData({ datalist: data,codtxt:res.codtxt});
				if (data.length == 0) {
					$.showEmpty('没有订单记录', '/images/empty2.png');
				}
			} else {
				if (data.length == 0) {
					that.setData({ $nomoreData: true });
				} else {
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
		});
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getDataList();
		}
	},
	onPullDownRefresh: function () {
		this.setData({ pagenum:1,datalist: [], $nomoreData: false })
		this.getDataList();
		wx.stopPullDownRefresh()
	},
	setst: function (e) {
		var that = this;
		var id = e.currentTarget.dataset.id
		var st = e.currentTarget.dataset.st
		if(st==1){
			var tips = '确定要改为正在配送中吗?';
		}else{
			var tips = '确定已完成配送吗?';
		}
		$.confirm(tips, function () {
			$.post('peisong/setst', { id: id,st:st }, function (data) {
				$.success(data.msg);
				setTimeout(function () {
					that.onLoad();
				}, 1000)
			}, '提交中');
		})
	},
	setpsst:function(e){
		var that = this;
		var st = e.currentTarget.dataset.st
		if(st==1){
			var tips = '确定要上线接单吗?';
		}else{
			var tips = '确定要暂停接单吗?';
		}
		$.confirm(tips, function () {
			$.post('peisong/setpsst', {st:st }, function (data) {
				$.success(data.msg);
				setTimeout(function () {
					that.onLoad();
				}, 1000)
			}, '提交中');
		})
	}
}))